/*******************************************************************************
 * Copyright (c) 2008, 2009 Fabio Mauro - Alberto Terraneo.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.2
 * or any later version published by the Free Software Foundation.
 *     
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Free Documentation License for more details.
 *     
 * You should have received a copy of the GNU Free Documentation 
 * License along with this distribution; if not, send a email to authors:
 * aterraneo <at> users.sourceforge.net
 * bigmoby09 <at> users.sourceforge.net
 *******************************************************************************/
package it.blueocean.acanto.dao.hibernate;

import it.blueocean.acanto.dao.MenuItemDao;
import it.blueocean.acanto.dao.NavigationItemDao;
import it.blueocean.acanto.dao.common.AGenericDaoHibernate;
import it.blueocean.acanto.model.MenuItem;
import it.blueocean.acanto.model.NavigationItem;

import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.inject.Inject;
import javax.inject.Named;

@Named("navigationItemDao")
public class NavigationItemDaoHibernate extends AGenericDaoHibernate<NavigationItem, Long> implements NavigationItemDao
{
	@Inject
	@Named("menuItemDao")
	private MenuItemDao menuItemDao;
	
	public SortedSet<NavigationItem> getOrderedMenu()
	{
		List<NavigationItem> listNavigationItems = findByQuery("from NavigationItem", null);

		SortedSet<NavigationItem> menu = createNavigator(listNavigationItems);

		return menu;
	}

	private SortedSet<NavigationItem> createNavigator(List<NavigationItem> listNavigationItems)
	{
		SortedSet<NavigationItem> setNavigator = new TreeSet<NavigationItem>();
		setNavigator.addAll(listNavigationItems);
		
		for (NavigationItem navigationItem : setNavigator)
		{
			SortedSet<MenuItem> children = menuItemDao.getOrderedMenuForNavigationItem(navigationItem);
			
			navigationItem.setChildren(children);
		}
		
		return setNavigator;
	}

}
